<template>
  <div class="login-container">
    <el-form :model="ruleForm2" :rules="rules2" v-if="type == '1'"
             status-icon
             ref="ruleForm2"
             label-position="left"
             label-width="0px"
             class="demo-ruleForm login-page">
      <h3 class="title">系统登录</h3>
      <el-form-item prop="username">
        <el-input type="text"
                  v-model="ruleForm2.username"
                  auto-complete="off"
                  placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password"
                  v-model="ruleForm2.password"
                  auto-complete="off"
                  placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-checkbox
              v-model="checked"
              class="rememberme"
      >记住密码</el-checkbox>
      <el-link class="rememberme" :underline="false" style="float:right" @click="changeType('2')"> 注册用户</el-link>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click="handleSubmit" :loading="logining">登录</el-button>
      </el-form-item>
    </el-form>

    <el-form :model="ruleForm2" :rules="rules2" v-if="type == '2'"
             status-icon
             ref="ruleForm2"
             label-position="left"
             label-width="0px"
             class="demo-ruleForm login-page">
      <h3 class="title">系统注册</h3>
      <el-form-item prop="username">
        <el-input type="text"
                  v-model="ruleForm2.username"
                  auto-complete="off"
                  placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password"
                  v-model="ruleForm2.password"
                  auto-complete="off"
                  placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password2">
        <el-input type="password"
                  v-model="ruleForm2.password2"
                  auto-complete="off"
                  placeholder="确认密码"
        ></el-input>
        <el-link class="rememberme" :underline="false" style="float:right" @click="changeType('1')">返回登录</el-link>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click="handleSubmit2" :loading="logining">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { login, register } from '@/api/login'
  import { setLoginName } from '@/utils/auth.js'
  export default {
    data(){
      return {
        type: '1',
        logining: false,
        ruleForm2: {
          username: '',
          password: '',
          password2: '',
        },
        rules2: {
          username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
          password: [{required: true, message: '请输入密码', trigger: 'blur'}]
        },
        checked: false
      }
    },
    methods: {
      changeType(type){
        this.ruleForm2 = {
          username: '',
          password: '',
          password2: '',
        },
        this.type = type
      },
      handleSubmit(){
        this.$refs.ruleForm2.validate((valid) => {
          if(valid){
            this.logining = true
            /*if(this.ruleForm2.username === 'admin' &&
                    this.ruleForm2.password === '123456'){
              this.logining = false;
              sessionStorage.setItem('user', this.ruleForm2.username);
              this.$router.push({path: '/'});*/
            login({'loginName':this.ruleForm2.username,'password':this.ruleForm2.password}).then(res => {
              this.logining = false;
              if(res.data.code === 200){
                setLoginName(res.data.data)
                this.$router.push({path: '/project'})
              }else{
                this.$message.error(res.data.msg + '')
              }
            })
          } else{
            console.log('error submit!');
            return false;
          }
        })
      },
      handleSubmit2(){
        this.$refs.ruleForm2.validate((valid) => {
          if(valid){
            if(this.ruleForm2.password != this.ruleForm2.password2) {
              this.$message.error('请确认输入的密码是否相同')
              return false
            }
            this.logining = true
            register({'loginName':this.ruleForm2.username,'password':this.ruleForm2.password}).then(res => {
              this.logining = false;
              if(res.data.code === 200){
                this.$message.success('注册成功')
                setTimeout(() => {
                  this.type = '1'
                }, 2000)
              }else{
                this.$message.error(res.data.msg + '')
              }
            })
          } else{
            console.log('error submit!');
            return false;
          }
        })
      }
    }
  };
</script>

<style scoped>
  .login-container {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-image: url("../assets/login-background.jpg");
    background-size: cover;
  }
  .login-page {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
  label.el-checkbox.rememberme {
    margin: 0px 0px 15px;
    text-align: left;
  }
</style>
